package com.xxx.dorm.web.servlet;


import com.xxx.dorm.domain.Building;
import com.xxx.dorm.domain.Dormitory;
import com.xxx.dorm.domain.Student;
import com.xxx.dorm.service.BuildingService;
import com.xxx.dorm.service.DormitoryService;
import com.xxx.dorm.service.StudentService;
import com.xxx.dorm.service.impl.BuildingServiceImpl;
import com.xxx.dorm.service.impl.DormitoryServiceImpl;
import com.xxx.dorm.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {

    private StudentService studentService = new StudentServiceImpl();
    private BuildingService buildingService = new BuildingServiceImpl();
    private DormitoryService dormitoryService = new DormitoryServiceImpl();

    /**
     * 查找所有学生
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String conditions = request.getParameter("conditions");
        String information = request.getParameter("information");
        String number = request.getParameter("number");
        List<Student> students = null;
        if(number != "" && number != null && information != null && information != ""){
            information = information + "#" + number;
            conditions = "buildingname_dormname";
            students = studentService.find(conditions,information);
        }else if(number != "" && number != null && (information == "" || information == null)){
            conditions = "number";
            information = number;
            students = studentService.find(conditions,information);
        }
        else if(conditions != "" && information != "" && conditions != null && information != null){
            students = studentService.find(conditions,information);
        }else{
            students = studentService.findAll();
        }
        for(int i=0;i<students.size();i++){
            Dormitory dormitory = dormitoryService.findByDid(students.get(i).getDid());
            Building building = buildingService.findByBid(dormitory.getBid());
            students.get(i).setBuilding(building);
            students.get(i).setDormitory(dormitory);
        }
        request.setAttribute("students",students);
        request.setAttribute("mainRight","/WEB-INF/jsp/student/studentList.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 添加学生
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void addStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String para = request.getParameter("para");
        if(para != null && para.equals("add")){
            request.setAttribute("mainRight","/WEB-INF/jsp/student/addStudent.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
            return;
        }
        Map<String, String[]> map = request.getParameterMap();
        Student student = new Student();
        BeanUtils.populate(student,map);
        studentService.addStudent(student);
        response.sendRedirect(request.getContextPath()+"/student/findAll");
    }

    /**
     * 查找某位同学(修改时用)
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void findOneStudent(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String id = request.getParameter("id");
        Student student = studentService.findById(Integer.valueOf(id));
        Dormitory dormitory = dormitoryService.findByDid(student.getDid());
        Building building = buildingService.findByBid(dormitory.getBid());
        student.setBuilding(building);
        student.setDormitory(dormitory);
        request.setAttribute("student",student);
        request.setAttribute("mainRight","/WEB-INF/jsp/student/updateStudent.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 修改
     * @param request
     * @param response
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws IOException
     */
    public void updateStudent(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Student student = new Student();
        BeanUtils.populate(student,map);
        Integer beforeDid = Integer.valueOf(request.getParameter("beforedid"));
        studentService.updateStudent(student,beforeDid);
        response.sendRedirect(request.getContextPath()+"/student/findAll");
    }

    /**
     * 删除学生
     * @param request
     * @param response
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws IOException
     */
    public void removeStudent(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException{
        String id = request.getParameter("id");
        studentService.removeStudent(id);
        response.sendRedirect(request.getContextPath()+"/student/findAll");
    }

}
